/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.prepay.service;

import java.util.List;

import com.jeesite.modules.custom.service.CodeRuleService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.prepay.entity.Tprepayment;
import com.jeesite.modules.prepay.dao.TprepaymentDao;
import com.jeesite.modules.prepay.entity.TprepaymentEstimated;
import com.jeesite.modules.prepay.dao.TprepaymentEstimatedDao;
import com.jeesite.modules.prepay.entity.TprepaymentActual;
import com.jeesite.modules.prepay.dao.TprepaymentActualDao;

/**
 * 预付款Service
 * @author wsm
 * @version 2021-03-18
 */
@Service
@Transactional(readOnly=true)
public class TprepaymentService extends CrudService<TprepaymentDao, Tprepayment> {
	
	@Autowired
	private TprepaymentEstimatedDao tprepaymentEstimatedDao;
	@Autowired
	private CodeRuleService codeRuleService;
	@Autowired
	private TprepaymentActualDao tprepaymentActualDao;
	@Autowired
	private  TprepaymentDao tprepaymentDao;
	/**
	 * 获取单条数据
	 * @param tprepayment
	 * @return
	 */
	@Override
	public Tprepayment get(Tprepayment tprepayment) {
		Tprepayment entity = super.get(tprepayment);
		if (entity != null){
			TprepaymentEstimated tprepaymentEstimated = new TprepaymentEstimated(entity);
			tprepaymentEstimated.setStatus(TprepaymentEstimated.STATUS_NORMAL);
			entity.setTprepaymentEstimatedList(tprepaymentEstimatedDao.findList(tprepaymentEstimated));
			TprepaymentActual tprepaymentActual = new TprepaymentActual(entity);
			tprepaymentActual.setStatus(TprepaymentActual.STATUS_NORMAL);
			entity.setTprepaymentActualList(tprepaymentActualDao.findList(tprepaymentActual));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param tprepayment 查询条件

	 * @return
	 */
	@Override
	public Page<Tprepayment> findPage(Tprepayment tprepayment) {
		return super.findPage(tprepayment);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param tprepayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(Tprepayment tprepayment) {
		
		if(tprepayment.getIsNewRecord()){
			tprepayment.setPrepaymentCode(codeRuleService.getCode("inboundorder001"));
			tprepayment.setCreateBy(UserUtils.getUser().getLoginCode());
			tprepayment.setStatus1("1");
			tprepayment.setBillStatus("1");
		}
		super.save(tprepayment);
		if (tprepayment.getIsNewRecord()){
			codeRuleService.updateCurrentNumber("inboundorder001");
		}
		// 保存 Tprepayment子表
		for (TprepaymentEstimated tprepaymentEstimated : tprepayment.getTprepaymentEstimatedList()){
			if (!TprepaymentEstimated.STATUS_DELETE.equals(tprepaymentEstimated.getStatus())){
				tprepaymentEstimated.setPrepaymentId(tprepayment);
				if (tprepaymentEstimated.getIsNewRecord()){
					tprepaymentEstimated.setPaymentAmountEstimated(tprepaymentEstimated.getPaymentSumActual());
					tprepaymentEstimatedDao.insert(tprepaymentEstimated);
				}else{
					tprepaymentEstimated.setPaymentAmountEstimated(tprepaymentEstimated.getPaymentSumActual()-tprepaymentEstimated.getPaymentAmountActual());
					tprepaymentEstimatedDao.update(tprepaymentEstimated);
				}
			}else{
				tprepaymentEstimatedDao.delete(tprepaymentEstimated);
			}
		}
		// 保存 Tprepayment子表
		for (TprepaymentActual tprepaymentActual : tprepayment.getTprepaymentActualList()){
			if (!TprepaymentActual.STATUS_DELETE.equals(tprepaymentActual.getStatus())){
				tprepaymentActual.setPrepaymentId(tprepayment);
				if (tprepaymentActual.getIsNewRecord()){
					tprepaymentActualDao.insert(tprepaymentActual);
				}else{
					tprepaymentActualDao.update(tprepaymentActual);
				}
			}else{
				tprepaymentActualDao.delete(tprepaymentActual);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param tprepayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(Tprepayment tprepayment) {
		super.updateStatus(tprepayment);
	}
	
	/**
	 * 删除数据
	 * @param tprepayment
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(Tprepayment tprepayment) {
		super.delete(tprepayment);
		TprepaymentEstimated tprepaymentEstimated = new TprepaymentEstimated();
		tprepaymentEstimated.setPrepaymentId(tprepayment);
		tprepaymentEstimatedDao.deleteByEntity(tprepaymentEstimated);
		TprepaymentActual tprepaymentActual = new TprepaymentActual();
		tprepaymentActual.setPrepaymentId(tprepayment);
		tprepaymentActualDao.deleteByEntity(tprepaymentActual);
	}
	//获取列表
	@Transactional(readOnly=false)
	public List<TprepaymentEstimated> findListBy(String supp ,String order) {
		List<TprepaymentEstimated> listBy = tprepaymentDao.findListBy(supp, order);
		return  listBy;
	}
	@Transactional(readOnly=false)
    public int updateFinancialRecognition(String apId, String sta) {
		int i =0;
		if("5".equals(sta)){
			Tprepayment tprepayment =new Tprepayment();
					tprepayment.setStatus1(sta);
					tprepayment.setPrepaymentId(apId);

					tprepaymentDao.update(tprepayment);
					i=1;
		}else{
			i = tprepaymentDao.updateFinancialRecognition(apId,sta);

			if("3".equals(sta)){
				tprepaymentDao.updateStatu(apId);
			}
		}

		return i;
    }
}